export default class Util {

    // 将金币格式化成可视数字.
    public static coinFormat(coin: number): string {
        if (coin < 1e3) {
            return `${Math.round(coin)}`
        } else if (coin < 1e6) {
            return `${(coin / 1e3).toFixed(2)} K`
        } else if (coin < 1e9) {
            return `${(coin / 1e6).toFixed(2)} M`
        } else if (coin < 1e12) {
            return `${(coin / 1e9).toFixed(2)} B`
        } else if (coin < 1e15) {
            return `${(coin / 1e12).toFixed(2)} T`
        } else if (coin < 1e18) {
            return `${(coin / 1e15).toFixed(2)} aa`
        } else if (coin < 1e21) {
            return `${(coin / 1e18).toFixed(2)} bb`
        } else if (coin < 1e24) {
            return `${(coin / 1e21).toFixed(2)} cc`
        } else if (coin < 1e27) {
            return `${(coin / 1e24).toFixed(2)} dd`
        } else if (coin < 1e30) {
            return `${(coin / 1e27).toFixed(2)} ee`
        } else if (coin < 1e33) {
            return `${(coin / 1e30).toFixed(2)} ff`
        } else if (coin < 1e36) {
            return `${(coin / 1e33).toFixed(2)} gg`
        } else {
            return `${(coin / 1e36).toFixed(2)} hh`
        }
    }

    // 格式化字符串. 格式为:${data}
    public static format(format: string, opts: any): string {
        if (!opts) {
            return format
        }
        let str = format;
        for (let key in opts) {
            if (opts.hasOwnProperty(key)) {
                str = str.replace(RegExp("%{\\s*" + key + "\\s*}", "g"), opts[key])
            }
        }
        return str
    }

    /**
     * 判断 两个rect 是否相交. 边重合不算相交.
     * @param rect
     * @param rect2
     */
    public static intersects(rect: cc.Rect, rect2: cc.Rect) {
        let maxax = rect2.x + rect2.width;
        let maxay = rect2.y + rect2.height;
        let maxbx = rect.x + rect.width;
        let maxby = rect.y + rect.height;
        return !(maxax <= rect.x || maxbx <= rect2.x || maxay <= rect.y || maxby <= rect2.y);
    };
}